<!DOCTYPE html>
<html>
<head>
	<% include ../link %>
</head>
<body>
	<% include ../head %>
	<div class="row">
		<div class="col-md-8">
			<ul class="nav nav-pills" role="tablist">
				<li role="presentation"><a href="/admin/video">视频管理</a></li>
				<li role="presentation" class="active"><a href="#">文章管理</a></li>
				<li role="presentation"><a href="/admin/users">用户管理</a></li>
				<li role="presentation"><a href="/admin/add">添加管理员</a></li>
			</ul>
		</div>
		<div class="col-md-2">
			<input class="form-control" type="text" name="nnn">
		</div>
		<div class="col-md-1">
			<button class="button button-action button-rounded button-small">select</button>
		</div>
	</div>
	
	<table class="table table-striped">
		<thead>
			<tr>
				<th>id</th>
				<th>name</th>
				<th>tag</th>
				<th>upUser</th>
				<th>upTime</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody data-bind="foreach: article">
			<tr>
				<th data-bind="text: id"></th>
				<th data-bind="text: name"></th>
				<th data-bind="text: tag"></th>
				<th data-bind="text: upUser"></th>
				<th data-bind="text: upTime"></th>
				<th>
					<button data-bind="attr: {title : id}" class="pass button button-action button-rounded button-small">pass</button>
					<button data-bind="attr: {title : id, value : img}" class="delete button button-caution button-rounded button-small">delete</button>
				</th>
			</tr>
		</tbody>
	</table>
</body>
<script type="text/javascript">

	var article = '<%- JSON.stringify(article)%>'
	article = JSON.parse(article)
	console.log(article)

	function getTime(time){
		time = parseInt(time);
		time = new Date(time).format("yyyy-MM-dd hh:ss");
		return time;
	}

	function ViewModel(){
		var self = this
		self.article = ko.observableArray()
	}

	var vm = new ViewModel();

	function articleModel(o){
		var self = this
		self.id = o._id
		self.name = o.name
		self.img = o.img
		self.tag = o.tag
		self.upUser = o.upUser.name
		self.upTime = getTime(o.upTime)
	}

	article.forEach(function(value, index){
		vm.article.push(new articleModel(value))
	})

	ko.applyBindings(vm)


	var deleteBtn = $('.delete');
	var passBtn = $('.pass');

	deleteBtn.on('click', function(){
		deleteAjax($(this), $(this).attr('title'), $(this).val());
	})

	passBtn.on('click', function(){
		passAjax($(this), $(this).attr('title'));
	})


	function deleteAjax(bt, id, img){
		$.ajax({
			type:'delete',
			url:'/admin/article',
			cache:false,
			data:{
				'id':id,
				'img':img
			},
			success:function(data){
				bt.parent().parent().remove()
			}
		})

	}

	function passAjax(bt, id){
		$.ajax({
			type:'put',
			url:'/admin/article',
			data:{
				'id':id
			},
			cache:false,
			success:function(data){
				bt.parent().parent().remove()
				// console.log(data)
			}
		})
	}
</script>
</html>